package br.com.vagacerta.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import br.com.vagacerta.dao.CupomDAO;
import br.com.vagacerta.modelo.Cupom;

@Controller
public class RelatorioController {

	private final CupomDAO cupomDAO;

	@Autowired
	public RelatorioController(CupomDAO cupomDAO) {
		this.cupomDAO = cupomDAO;
	}

	@RequestMapping("consultaCupomPorData")
	public String consultaCupomPorData(String dataInicial, String dataFinal,
			Model model) {
		Calendar dateInicial = Calendar.getInstance();
		Calendar dateFinal = Calendar.getInstance();

		try {

			SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
			dateInicial.setTime(sdf.parse(dataInicial));
			dateInicial.set(Calendar.HOUR, 00);
			dateInicial.set(Calendar.MINUTE, 00);
			dateInicial.set(Calendar.SECOND, 00);
			dateFinal.setTime(sdf.parse(dataFinal));
			dateFinal.set(Calendar.HOUR, 23);
			dateFinal.set(Calendar.MINUTE, 59);
			dateFinal.set(Calendar.SECOND, 59);
		} catch (ParseException e) {
			e.printStackTrace();
		}
			
		List<Cupom> listaCupom = cupomDAO.retornaListaCuponsPorData(dateInicial, dateFinal);
		
		model.addAttribute("listaCupom", listaCupom);

		return "relatorios/tabelaRelatorioCupons";

	}

}
